package com.itboye.mengwu.app.ui.mymeng;

import java.util.Observable;
import java.util.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.buy.ui.DaiFuKuanDetail;
import com.itboye.mengwu.data.model.OrderDetailBean;
import com.itboye.mengwu.data.presenter.OrderPresenter;

public class ActivityTuiHuoTuiKuan extends BaseActivity implements
		OnClickListener, Observer {
	TextView txt_title;
	ImageView back, goods_im;
	TextView goods_name, goods_price, huanhuo;
	EditText why, num, shuoming;
	Button tijiao;
	private String reason;
	private String note;
	private String money;
	private OrderPresenter orderpresenter;
	private String uid;
	private String orderCode;
	OrderDetailBean orderDetailBean;
	LinearLayout goodsGroup;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_tuikuanhuanhuo;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txt_title.setText("退款退货");
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		orderDetailBean = (OrderDetailBean) getIntent().getSerializableExtra(
				"model");
		for (int i = 0; i < orderDetailBean.getItems().size(); i++) {
			View view = LayoutInflater.from(ActivityTuiHuoTuiKuan.this)
					.inflate(R.layout.shouhou_goodsitem, null);
			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
					+ orderDetailBean.getItems().get(i).getImg(),
					((ImageView) view.findViewById(R.id.goods_im)));
			((TextView) view.findViewById(R.id.goods_name))
					.setText(orderDetailBean.getItems().get(i).getName());
			((TextView) view.findViewById(R.id.goods_price)).setText("￥"
					+ orderDetailBean.getItems().get(i).getPrice());
			goodsGroup.addView(view);
		}
		orderCode = orderDetailBean.getOrderCode();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.tijiao:
			/**
			 * uid
			 * 
			 * yes
			 * 
			 * 用户ID
			 * 
			 * order_code
			 * 
			 * yes
			 * 
			 * 订单编码
			 * 
			 * reason
			 * 
			 * 原因
			 * 
			 * refund_type
			 * 
			 * yes
			 * 
			 * 售后类型， 1=>退款,2=>退货,3=>退货退款
			 * 
			 * goods_get
			 * 
			 * yes
			 * 
			 * 是否后到货物 1=>收到货物,2=>未收到
			 * 
			 * note
			 * 
			 * 说明
			 * 
			 * refund_money
			 * 
			 * yes
			 * 
			 * 退款金额，default:0
			 */
			reason = why.getText().toString();
			money = num.getText().toString();
			note = shuoming.getText().toString();
			orderpresenter = new OrderPresenter(this);
			orderpresenter.shenQingShouHou(uid, orderCode, money, reason,
					"3",true,note);
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == OrderPresenter.ShenQingSuccess) {
				ByAlert.alert(entity.getData() + "");
				finish();
			} else if (entity.getEventType() == OrderPresenter.ShenQingFail) {
				ByAlert.alert(entity.getData() + "");
				// finish();
			}
		}
	}

}
